<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class ShopBrandModel extends Model
{
    //定义一个表名
	protected $table = 'shop_brand';
	protected $guarded = [];
	//定义主键
	protected $primaryKey = 'id';

	static function letter($keywords){
		$data = [];
		$list = ShopBrandModel::Select('head')->orderBy('head')->distinct(true)->where('name','like',"%".$keywords."%")->get()->toArray();
		$aa = '';
		foreach($list as $k=>$v){
			$aa .= $v['head'].",";
		}
		$aa = explode(',',trim($aa,','));
		foreach($aa as $k=>$v){
			$data[$k]['head'] = $v;
			$data[$k]['user'] = ShopBrandModel::letter_re($v,$keywords);
		}
		return $data;
	}

	static function letter_re($letter,$keywords){
		$data = [];
		$list = ShopBrandModel::OrderBy(DB::raw('convert(`name` using gbk)'))->select('bid','picname','picpath','name','oss_pic')->where('name','like',"%".$keywords."%")->where('head',$letter)->get()->toArray();
		foreach($list as $k=>$v){
			$data[$k]['name'] = $v['name'];
			$data[$k]['id'] = $v['bid'];
			//头像
			if($v['oss_pic']){
				$data[$k]['picname'] = $v['oss_pic'];
			}else{
				if($v['picname']){
					$data[$k]['picname'] = "http://".$_SERVER['HTTP_HOST'].$v['picpath'].$v['picname'];	
				}else{
					$data[$k]['picname'] = "http://".$_SERVER['HTTP_HOST'].'/img/pinpai.png';
				}
			}
			
		}
		return $data;
	}

	static function getList($keywords){
		$data = [];
		$list = ShopBrandModel::select('id','picname','picpath','name','bid','head','oss_pic')->where('name','like',"%".$keywords."%")->get()->toArray();
		foreach($list as $k=>$v){
			$data[$k]['name'] = $v['name'];
				$data[$k]['id'] = $v['bid'];
				$data[$k]['head'] = $v['head'];
			//头像
			if($v['oss_pic']){
				$data[$k]['picname'] = $v['oss_pic'];
			}else{
				if($v['picname']){
					$data[$k]['picname'] = "http://".$_SERVER['HTTP_HOST'].$v['picpath'].$v['picname'];
				}else{
					$data[$k]['picname'] = "http://".$_SERVER['HTTP_HOST'].'/img/pinpai.png';
				}
			}
			
		}
		return $data;
	}

}
